import legacyPlugin from "@vitejs/plugin-legacy"
import * as path from "path"
import * as dotenv from "dotenv"
import * as fs from "fs"
import vuePlugin from "@vitejs/plugin-vue"
import vueDevTools from "vite-plugin-vue-devtools"
import { svgBuilder } from "vite-auto-import-svg"

import svgLoader from "vite-svg-loader"
import vueJsx from "@vitejs/plugin-vue-jsx"
// import UnoCSS from 'unocss/vite';
// import { presetUno } from 'unocss';

import { quasar, transformAssetUrls } from "@quasar/vite-plugin"

export default ({ command, mode }) => {
    const NODE_ENV = mode || "development"
    const envFiles = [`.env.${NODE_ENV}`]
    for (const file of envFiles) {
        const envConfig = dotenv.parse(fs.readFileSync(file))
        for (const k in envConfig) {
            process.env[k] = envConfig[k]
        }
    }
    const timestamp = Date.parse(new Date())
    const optimizeDeps = {}
    const alias = {
        "@": path.resolve(__dirname, "./src"),
        vue$: "vue/dist/vue.runtime.esm-bundler.js",
    }
    const esbuild = {}

    const rollupOptions = {
        output: {
            entryFileNames: "assets/[name].[hash].js",
            chunkFileNames: "assets/[name].[hash].js",
            assetFileNames: "assets/[name].[hash].[ext]",
        },
    }

    const config = {
        base: "/kaimom-su-admin/", // 编译后js导入的资源路径
        root: "./", // index.html文件所在位置
        publicDir: "public", // 静态资源文件夹
        resolve: {
            alias,
        },
        define: {
            "process.env": {},
        },
        css: {
            preprocessorOptions: {
                scss: {
                    // api: "modern-compiler", // or "modern"
                    additionalData: `@use "@/css/amtf-var.scss" as *;`,
                },
            },
        },
        server: {
            // 如果使用docker-compose开发模式，设置为false
            open: true,
            // allowedHosts: 'all',
            allowedHosts: ["6xv7xt.natappfree.cc", "s2xdk2.natappfree.cc"],
            port: process.env.VITE_CLI_PORT,
            proxy: {
                // 把key的路径代理到target位置
                // detail: https://cli.vuejs.org/config/#devserver-proxy
                [process.env.VITE_BASE_API]: {
                    // 需要代理的路径   例如 '/api'
                    target: `${process.env.VITE_BASE_PATH}:${process.env.VITE_SERVER_PORT}/`, // 代理到 目标路径
                    changeOrigin: true,
                    rewrite: path => path.replace(new RegExp("^" + process.env.VITE_BASE_API), ""),
                },
            },
        },
        build: {
            // target: "es2015",
            minify: "terser", // 是否进行压缩,boolean | 'terser' | 'esbuild',默认使用terser
            manifest: false, // 是否产出manifest.json
            // sourcemap: false, // 是否产出sourcemap.json
            sourcemap: true, // 是否产出sourcemap.json
            // sourcemap: 'inline',//将源代码映射直接嵌入到编译后的文件中。
            outDir: "dist/kaimom-su-admin", // 产出目录
            terserOptions: {
                compress: {
                    //生产环境时移除console
                    drop_console: true,
                    drop_debugger: true,
                },
            },
            rollupOptions,
        },
        esbuild,
        optimizeDeps,
        plugins: [
            // UnoCSS({
            //     presets: [presetUno()],
            //     safelist: ["bg-#545c64", "bg-#000c17"],
            // }),
            vuePlugin({
                template: { transformAssetUrls },
            }),
            quasar({
                // 在vue组件中注入，而不会在单独的css文件中注入👇
                sassVariables: "@/css/amtf-var.scss",
            }),
            svgLoader(),
            vueJsx(),
            process.env.VITE_POSITION === "open" && vueDevTools({ launchEditor: process.env.VITE_EDITOR }),
            legacyPlugin({
                targets: [
                    "Android > 39",
                    "Chrome >= 60",
                    "Safari >= 10.1",
                    "iOS >= 10.3",
                    "Firefox >= 54",
                    "Edge >= 15",
                ],
            }),
        ],
    }
    return config
}
